package org.example.forum.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.forum.common.AppResult;
import org.example.forum.common.ResultCode;
import org.example.forum.dao.BoardMapper;
import org.example.forum.exception.ApplicationException;
import org.example.forum.model.Article;
import org.example.forum.model.Board;
import org.example.forum.service.IBoardService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class BoardServiceImpl implements IBoardService {
    @Resource
    private BoardMapper boardMapper;

    @Override
    public List<Board> selectByNum(Integer num) {
        //非空校验
        if(num<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failure(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        return boardMapper.selectByNum(num);
    }

    @Override
    public void addOneArticleCountById(Long id) {
        if(id==null || id<=0){
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(AppResult.failure(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        //查询到对应的板块
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board==null){
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString()+"board id:{}"+id);
            throw new ApplicationException(AppResult.failure(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        //更新板块帖子数量
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount()+1);
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        if(row != 1){
            log.warn(ResultCode.FAILED.toString()+"受影响的行数大于1");
            throw new ApplicationException(AppResult.failure(ResultCode.FAILED));
        }
    }

    @Override
    public void subOneArticleCountById(Long id) {
        if(id==null || id<=0){
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(AppResult.failure(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        //查询到对应的板块
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board==null){
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString()+"board id:{}"+id);
            throw new ApplicationException(AppResult.failure(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        //更新板块帖子数量
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount()-1);
        //如果小于0 更新为0
        if(updateBoard.getArticleCount()<0){
            updateBoard.setArticleCount(0);
        }
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        if(row != 1){
            log.warn(ResultCode.FAILED.toString()+"受影响的行数大于1");
            throw new ApplicationException(AppResult.failure(ResultCode.FAILED));
        }
    }

    @Override
    public Board selectById(Long id) {
        if(id==null || id<=0){
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(AppResult.failure(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        //查询到对应的板块
        Board board = boardMapper.selectByPrimaryKey(id);
        return board;
    }

}
